<template>
  <div>
  <div class="bgfff mtrbl20 ptrbl20 nopb20">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
          <el-form-item label="姓名" prop="realName">
            <el-input
              v-model="queryParams.realName"
              placeholder="请输入姓名"
              clearable
              size=""
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="证件号" prop="idNumber" label-width="68px">
            <el-input
              v-model="queryParams.idNumber"
              placeholder="请输入证件号"
              clearable
              size=""
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />  
          </el-form-item>
          <el-form-item label="纳税人类型" prop="taxIdentity">
            <el-select
              v-model="queryParams.taxIdentity"
              placeholder="全部"
              clearable
              multiple
              collapse-tags
              style="width: 180px"
            >
              <el-option label="个体工商户" :value="1"></el-option>
              <el-option label="临时税务登记" :value="0"></el-option>
              <el-option label="自然人" :value="88"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="申报状态" prop="declareStatus">
            <el-select
              v-model="queryParams.declareStatus"
              placeholder="全部"
              clearable
              style="width: 150px"
            >
              <el-option label="未申报" value="0"></el-option>
              <el-option label="部分申报" value="2"></el-option>
              <el-option label="已申报" value="3"></el-option>
              <el-option label="申报失败" value="4"></el-option>
              <el-option label="申报数据异常" value="5"></el-option>
            </el-select>
          </el-form-item>
        
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="medium"
              @click="handleExport"
              v-hasPermi="['taxmsg:declare:dataDownload']"
            >导出账单</el-button>
            <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="medium"
            :disabled="multiple"
            @click="handleBatchDownloadFile"
          >批量下载凭证</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      </el-row> 
    </div> 
        <!-- <el-row :gutter="10" class="mb8">
         
          <el-col :span="1.5">
           
          </el-col>
         <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">  
        <el-table v-loading="loading" :data="declareDataList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column fixed label="序号" prop="" width="55" align="center">
            <template slot-scope="scope">
              <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属客户企业" align="left" key="companyNames" prop="companyNames" :show-overflow-tooltip="true"/>
          <el-table-column label="管理企业" align="left" key="companyAdminNames" prop="companyAdminNames" :show-overflow-tooltip="true"/>
          <el-table-column label="姓名" align="left" key="realName" prop="realName" />
          <el-table-column label="证件号" align="left" key="idNumber" prop="idNumber" />
          <el-table-column label="税号/临登号" align="left" key="taxNo" prop="taxNo" :show-overflow-tooltip="true"/>
          <el-table-column label="申报日期" align="left" key="months" prop="months" />
          <el-table-column label="应当申报金额" align="left" key="totalMoney" prop="totalMoney" />
          <el-table-column label="实际申报金额" align="left" key="actualMoney" prop="actualMoney" />
          <el-table-column label="完税状态" align="left" key="declareStatus.name" prop="declareStatus.name" />
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
              <el-dropdown size="mini" split-button type="primary" @click="handleView(scope.row)" placement="bottom-start" trigger="click">
                详 情
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-edit" @click.native="handleDeclareDataUpdate(scope.row)" v-hasPermi="['taxmsg:declare:dataUpdate']">编辑状态</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-upload2" @click.native="handleUpload(scope.row)" v-hasPermi="['taxmsg:declare:fileUpload']">上传凭证</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-download" @click.native="handleDownload(scope.row)" v-hasPermi="['taxmsg:declare:fileList']">下载凭证</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleDeclareDataUpdate(scope.row)"
            >编辑状态</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload2"
              @click="handleUpload(scope.row)"
            >上传凭证</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-download"
              @click="handleDownload(scope.row)"
            >下载凭证</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 编辑状态对话框 -->
    <el-dialog :title="title" :visible.sync="openDeclareDataUpdate" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申报状态" prop="declareStatus">
          <el-select
            v-model="form.declareStatus"
            placeholder="全部"
            clearable
            size="small"
          >
            <el-option label="申报中" value="1"></el-option>
            <el-option label="已申报完税" value="3"></el-option>
            <el-option label="申报失败" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申报金额" prop="actualMoney" v-if="form.declareStatus == 3">
          <el-input style="width: 215px" v-model="form.actualMoney" clearable placeholder="请输入实际申报金额" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelDeclareDataUpdate">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 上传凭证对话框 -->
    <el-dialog title="上传凭证" :visible.sync="openProof" width="400px" append-to-body>
      <el-upload
      ref="upload1"
      action=""
      :limit=limitNum
      :auto-upload="false"
      accept="*"
      :before-upload="beforeUploadFile"
      multiple
      :on-exceed="exceedFile"
      :on-success="handleSuccess"
      :file-list="fileList"
      drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <!-- <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div> -->
        <!-- <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div> -->
      </el-upload>
      <el-form class="mt20">
        <el-form-item prop="remark">
          <el-input type="textarea" v-model="remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm1">确 定</el-button>
        <el-button @click="cancelProof">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 申报资料列表对话框 -->
    <el-dialog title="申报资料列表" :visible.sync="openDeclareFileList" width="1100px" append-to-body>
      <el-table v-loading="loading" :data="declareFileList">
        <el-table-column fixed label="序号" prop="" width="55" align="center">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="文件名" align="left" key="fileName" prop="fileName" width="200"/>
        <el-table-column label="文件后缀" align="left" key="fileSuffix" prop="fileSuffix"/>
        <el-table-column label="文件大小（kb）" align="left" key="fileSize" prop="fileSize"/>
        <el-table-column label="上传人名称" align="left" key="createByName" prop="createByName"/>
        <el-table-column label="上传时间" align="left" key="createTime" prop="createTime" width="200"/>
        <el-table-column label="备注" align="left" key="remark" prop="remark"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownloadFile(scope.row)"
          >下载凭证</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDeclareFileList">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
import { declareDataList, declareDataUpdate, declareDataDownload, declareFileBatchUpload, declareFileList, declareFileListFileKey } from "@/api/taxmsg/declareDetails";
import { taxOssAliyunSts } from "@/api/oss";
export default {
  name: "DeclareDetails",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 用户表格数据
      declareDataList: null,
      // 申报资料列表
      declareFileList: null,
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（凭证）
      openProof: false,
      // 是否显示弹出层（编辑状态）
      openDeclareDataUpdate: false,
      //是否显示弹出框（申报资料列表）
      openDeclareFileList: false,
      form: {}, 
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        realName: undefined,
        idNumber: undefined,
        taxIdentity: undefined,
        declareStatus: undefined,
        batchId: this.$route.params && this.$route.params.id
      },
      // 编辑状态表单校验
      rules: {
        declareStatus: [
          { required: true, message: "请选择申报状态", trigger: "blur" }
        ],
        actualMoney: [
          { required: true, message: "实际开票金额不能为空", trigger: "blur" }
        ]
      },
      remark:'',
      limitNum: 5,
      fileList: [],
     
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.declareStatus==""){
        this.queryParams.declareStatus = null
      }
      declareDataList(this.queryParams).then(response => {
          this.declareDataList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 详情按钮操作 */
    handleView(row) {
      const id = row.id || this.ids[0];
      this.$router.push("/declare/details2/" + id);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          if(queryParams.declareStatus==""){
                queryParams.declareStatus = null
          }
          return declareDataDownload(queryParams);
        }).then(response => {
          //this.download(response.msg);
          var list_name = "完税申报";
          this.export_excel_file(response, list_name);
        }).catch(()=>{})
    },
    cancelDeclareDataUpdate() {
      this.openDeclareDataUpdate = false;
      this.reset();
    },
    reset() {
      this.form = {
        declareStatus: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 编辑状态按钮操作 */
    handleDeclareDataUpdate(row) {
      this.reset();
      this.form.declareId= row.id;
      this.form.batchId= this.$route.params && this.$route.params.id;

      //this.form.declareStatus = row.declareStatus.name;
      if(row.declareStatus.name=="申报中"){
        this.form.declareStatus = "1";
      }else if(row.declareStatus.name=="已申报完税"){
        this.form.declareStatus = "3";
      }else if(row.declareStatus.name=="申报失败"){
        this.form.declareStatus = "4";
      }
      //this.form1.userName= row.userName;
      this.openDeclareDataUpdate = true;
      this.title = "编辑状态";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          declareDataUpdate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.openDeclareDataUpdate = false;
              this.getList();
            }).catch(()=>{});
        }
      });
    },
    cancelProof() {
        this.openProof = false;
    },
    handleUpload(row) {
        this.openProof = true;
        this.form.declareId = row.id;
        this.form.batchId = row.batchId;
    },
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: `只能选择 ${this.limitNum} 个文件，当前共选择了 ${files.length + fileList.length} 个`
      });
    },
    // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject，则停止上传
    beforeUploadFile(file) {
      //console.log(file)
      const size = file.size / 1024 / 1024
      if(size > 200) {
        this.$notify.warning({
          title: '警告',
          message: `文件大小不得超过200M`
        });
      }
      const fd = new FormData()
      fd.append('file', file)
      fd.append('declareId', this.form.declareId)
      fd.append('batchId', this.form.batchId)
      if(this.remark)
      fd.append('remark', this.remark)
  
      declareFileBatchUpload(fd).then(response => {
          this.openProof = false;
          this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
          this.getList();
      }).catch(()=>{});
      return true
    },
    // 文件上传成功时的钩子
    handleSuccess(files, fileList) {
      this.$notify.success({
        title: '成功',
        message: `文件上传成功`
      });
    },
    submitFileForm1() {
      this.$refs.upload1.submit()
    },
    cancelDeclareFileList() {
      this.openDeclareFileList = false;
    },
    handleDownload(row) {
      //this.form.invoiceId= row.id;
      this.openDeclareFileList = true;
      this.loading = true;
      declareFileList(row.id).then(response => {
          this.declareFileList = response.data;
          this.loading = false;
        }
      );
    },
    handleDownloadFile(row) {
      this.getTaxOssAliyunSts(row.fileKey);
    },
    handleBatchDownloadFile() {
      const id = this.ids;
      this.$confirm('是否确认下载凭证编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          //return declareFileListFileKey({declareIds:id});
         declareFileListFileKey({declareIds:id}).then(response => {
            // taxOssAliyunSts({bucket:'tax-backends'}).then(res => {
            //   var OSS = require("ali-oss");
            //   var client = new OSS({
            //     region: 'oss-cn-shenzhen',
            //     accessKeyId: res.data.accessKeyId,
            //     accessKeySecret: res.data.accessKeySecret,
            //     stsToken: res.data.securityToken,
            //     bucket: 'tax-backends'
            //   });
            //   response.data.forEach(item => {
            //     var url = client.signatureUrl(item.fileKey);
            //     //console.log(url);
            //     window.location.href = url;
            //   });
            // }); 
          
         });
        }).catch(()=>{})
    }
  }
};
</script>
<style scoped lang="scss">
</style>